package com.itheima.mall.web.sms;

import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.sms.HomeRecommendProductService;
import com.itheima.mall.entity.domain.sms.HomeRecommendProduct;
import com.itheima.mall.entity.domain.vo.smsre.ResultAll;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@ResponseBody
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/home/recommendProduct")

public class HomeRecommendProductController {
    @DubboReference
    private HomeRecommendProductService homeRecommendProductService;

    @PostMapping("/create")
    public Result create(@RequestBody List<HomeRecommendProduct> homeRecommendProducts) {
        for (HomeRecommendProduct homeRecommendProduct : homeRecommendProducts) {
            homeRecommendProductService.save(homeRecommendProduct);
        }
        System.out.println("0000000000000000");
        return Result.success("成功");
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam List<Long> ids) {
        System.out.println("22222222222");
        for (Long id : ids) {
            homeRecommendProductService.removeById(id);
        }
        System.out.println("22222222222");
        return Result.success("成功");

    }

    @PostMapping("/update/recommendStatus")
    @ResponseBody
    public Result updaterecommendStatus(@RequestParam List<Long> ids, @RequestParam Integer recommendStatus) {
        List<HomeRecommendProduct> homeRecommendProductList = homeRecommendProductService.listByIds(ids);
        for (HomeRecommendProduct homeRecommendProduct : homeRecommendProductList) {
            homeRecommendProduct.setRecommendStatus(recommendStatus);
            homeRecommendProductService.updateById(homeRecommendProduct);
        }
        return Result.success("成功");
    }
    @RequestMapping("/update/sort/{id}")
    public Result updateSort(@PathVariable Long id, Integer sort) {
        System.out.println(id);
        System.out.println(sort);
        homeRecommendProductService.updateSort(id, sort);
        return Result.success();
    }
    @GetMapping("list")
    public Result list(
            @RequestParam(value = "brandName", required = false) String productName,
            @RequestParam(value = "recommendStatus", required = false) Integer recommendStatus,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value = "pageNum") Integer pageNum
    ) {
        ResultAll resultAll = homeRecommendProductService.page(pageNum, pageSize,recommendStatus,productName);
        return Result.success(resultAll);
    }

}
